package com.lp.article.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lp.article.service.ICommentService;
import com.lp.model.config.AppResult;
import com.lp.model.config.CodeMsg;
import com.lp.model.dto.article.PageDto;
import com.lp.model.entity.article.Comments;
import com.lp.model.entity.article.redis.vo.CommentsMsgVO;
import com.lp.model.entity.article.redis.vo.LikesMsgVO;
import com.lp.model.entity.article.vo.CommentsVO;
import com.lp.model.entity.article.vo.PageVO;
import com.lp.model.entity.user.User;
import com.lp.model.utils.PageUtils;
import com.lp.model.utils.UserUtils;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/article/comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 增加评论
     * @param request
     * @param comments 文章 id 和 评论内容
     * @return
     */
    @PostMapping("/add")
    public AppResult add(HttpServletRequest request,
                                @RequestBody @NonNull Comments comments) {
        //1.非空校验
        if(comments.getArticleId() == null || comments.getContent() == null) {
            return AppResult.fail(CodeMsg.FAIL_NOT_NULL_COMMENT);
        }
        //2.非法校验
        if(comments.getArticleId() <= 0) {
            return AppResult.fail(CodeMsg.FAIL_ERROR_COMMENT);
        }
        //3.准备用户信息
        User userByToken = UserUtils.getUserByToken(redisTemplate, objectMapper, request);
        comments.setUserId(userByToken.getId());
        commentService.add(comments);
        return AppResult.success();
    }

    /**
     * 删除评论
     * @param id 评论id
     * @return
     */
    @GetMapping("/remove")
    public AppResult remove(@NonNull Long id) {
        //1.非法校验
        if(id <= 0) {
            return AppResult.fail(CodeMsg.FAIL_NOT_NULL_COMMENT);
        }
        //2.删除评论
        commentService.remove(id);
        return AppResult.success();
    }

    /**
     * 获取文章的评论列表
     * @param page 分页数据 + 文章 id
     */
    @PostMapping("/page")
    public AppResult list(@RequestBody @NonNull PageDto page) {
        //1.非法校验
        if(!PageUtils.checkPageDto(page)) {
            return AppResult.fail(CodeMsg.FAIL_ERROR_ARTICLE_PAGE);
        }
        //2.获取评论列表
        PageVO<CommentsVO> commentVOPage = commentService.getCommentVOPage(page);
        return AppResult.success(commentVOPage);
    }

    /**
     * 删除该用户的所有点赞消息
     */
    @GetMapping("/del_msg_all")
    public AppResult delMsgAll(HttpServletRequest request) {
        //1.准备用户信息
        User userByToken = UserUtils.getUserByToken(redisTemplate, objectMapper, request);
        Long userId = userByToken.getId();
        commentService.delMsgAll(userId);
        return AppResult.success();
    }

    @PostMapping("/msg_page")
    public AppResult msgPage(HttpServletRequest request, @NonNull @RequestBody PageDto page) {
        //1.非法校验
        if(!PageUtils.checkPageDto(page)) {
            return AppResult.fail(CodeMsg.FAIL_ERROR_PAGE);
        }
        //2.准备用户数据
        User user = UserUtils.getUserByToken(redisTemplate, objectMapper, request);
        page.setId(user.getId());
        PageVO<CommentsMsgVO> msgPage = commentService.getMsgPage(page);
        return AppResult.success(msgPage);
    }

    @GetMapping("/is_read")
    public AppResult isRead(HttpServletRequest request) {
        //1.准备用户数据
        User user = UserUtils.getUserByToken(redisTemplate, objectMapper, request);
        boolean read = commentService.isRead(user.getId());
        return AppResult.success(read);
    }

}
